package com.cls.business.strategy.impl.notice;

import com.cls.business.entity.Notice;
import com.cls.business.entity.Standard;
import com.cls.business.entity.TopicPlan;
import com.cls.business.service.INoticeService;
import com.cls.business.service.IStandardService;
import com.cls.business.service.ITopicPlanService;
import com.cls.business.strategy.NoticeStrategy;
import com.cls.common.enums.DictEnum;
import com.cls.common.exception.MyException;
import com.cls.system.entity.Configure;
import com.cls.system.service.IConfigureService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;

/**
 * wqc
 * 课题指南  申报公告
 */
@Component("notice_type_ktzn")
@RequiredArgsConstructor
public class KTZNNoticeImpl implements NoticeStrategy {
    private final INoticeService noticeService;
    private final ITopicPlanService topicPlanService;

    private final IStandardService standardService;

    private final IConfigureService configureService;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void createNotice(Notice notice) {

        String year;
        if (StringUtils.isNotEmpty(notice.getYear())) {
            year = notice.getYear();
        } else {
            Calendar date = Calendar.getInstance();
            year = date.get(Calendar.YEAR) + "";
        }
        //根据年份获取计划
        TopicPlan topicPlan = topicPlanService.selectAnnualByYear(year);
        if (topicPlan == null) {
            throw new MyException("对不起，指定年份没有发布课题征集！");
        }
        if (topicPlan.getApplyNoticeId() != null) {
            throw new MyException("对不起，课题申报已创建不可重复创建！");
        }
        noticeService.save(notice);
        topicPlan.setApplyNoticeId(notice.getNoticeId());
        topicPlan.setApplySubmitEndTime(notice.getApplySubmitEndTime());
        topicPlan.setSubmitBeginTime(notice.getSubmitBeginTime());
        topicPlan.setIntentionSubmitEndTime(notice.getIntentionSubmitEndTime());

        //创建评审标准
        Standard standard = new Standard();
        standard.setSnapshot(false);
        Long standardId = standardService.snapshotStandard(standard);
        if ( standardId < 0 || standardId == null) {
            throw new MyException("请先配置评审标准！");
        }

        topicPlan.setStandardId(standardId);
        Configure configure = new Configure();
        configure.setSnapshot(false);
        Long configureId = configureService.snapshotConfigure(configure);
        if (standardId < 0 || configureId == null) {
            throw new MyException("请先配置评审规则！");
        }
        topicPlan.setConfigureId(configureId);
        configure = configureService.getById(configureId);
        String businessTypeCode = configure.getBusinessTypeCode();
        //有意向则必须填写意向表申请时间
        boolean isIntention = DictEnum.CONFIGURE_INTENTION.getCode().equals(businessTypeCode);
        if (isIntention && notice.getIntentionSubmitEndTime() == null) {
            throw new MyException("对不起，意向表提交结束时间不能为空！");
        }

        topicPlanService.updateById(topicPlan);
    }
}
